#type = "mysql+pymysql"
#username = "root"
#password = "123456"
#ipaddrsss = "127.0.0.1"
#port = 3306
#schema = "seproject"
# SQL Server 配置（修改部分）
#type = "mssql+pyodbc"  # 或 "mssql+pymssql"
#username = "sa"        # SQL Server 默认管理员账号
#password = "123456"
#ipaddress = "127.0.0.1"  # 或 SQL Server 实例名，如 "localhost\SQLEXPRESS"
#port = 1433            # SQL Server 默认端口
schema = "seproject"  # 数据库名
import pymssql


# 第一部分：创建表
def create_table():
    try:
        connect = pymssql.connect(host='LAPTOP-DEUDG1NI',  # 只需要host或server中的一个
                                  port='1433',
                                  user='yunmengze',
                                  password='123456',
                                  database='soft2104')

        cursor = connect.cursor()
        cursor.execute("create table C_test02(id varchar(20))")
        connect.commit()
        print("表创建成功！")
    except Exception as e:
        print(f"创建表时出错: {e}")
    finally:
        if 'cursor' in locals():
            cursor.close()
        if 'connect' in locals():
            connect.close()


# 第二部分：查询数据
def query_data():
    try:
        connect = pymssql.connect(host='LAPTOP-DEUDG1NI',
                                  port='1433',
                                  user='yunmengze',
                                  password='123456',
                                  database='soft2104')

        if connect:
            print("数据库连接成功！")

        cursor = connect.cursor()
        sql = "select * from dbo.T_class"
        cursor.execute(sql)

        # 获取所有结果并打印
        rows = cursor.fetchall()
        for row in rows:
            print(row)  # 直接打印整行，避免索引越界

    except Exception as e:
        print(f"查询数据时出错: {e}")
    finally:
        if 'cursor' in locals():
            cursor.close()
        if 'connect' in locals():
            connect.close()


# 调用函数
create_table()
query_data()

#SQLALCHEMY_DATABASE_URL = f"{type}://{username}:{password}@{ipaddrsss}:{port}/{schema}"
imgprefix = "http://127.0.0.1:8000/source/"
testopenid = "oxxHL5GS26iMW6iSMzfoLjXk1luw"
imgpath = "../wxfile/source/seproject/"

wxurl = "https://api.weixin.qq.com/sns/jscode2session"
wxappid = "wx27cc35bd452e6ec4"
wxsecret = "11dcbb7eb2d66961d8efe970de02c2fa"

# 在终端中通过uvicorn启动
# uvicorn seproject_app.main:app --reload